<?php
require('../../../lib/base.php');
$base=new base(array('web','mail'));
$helper=$base->library('helper');
if($base->post('reset',true)){
	$user=$base->post('member',true);
	$code=$base->post('code',true);
	$password=$base->post('password',true);
	if(strlen($password)<6||strlen($password)>32){
		$helper->SMessage(5,$_SERVER['HTTP_REFERER'],"no","您输入的密码有误,5秒返回上一页.");
		exit();
	}
	if(!is_numeric($user)||strlen($code)!=32){
		$helper->SMessage(5,$_SERVER['HTTP_REFERER'],"no","您的链接有误,请务修改链接.");
		exit();
	}
	$member=$base->model('member');
	$user=reset($member->GMember(array('select'=>array('id','password','key','visits'),'where'=>array('id ='=>$user))));
	if(empty($user)){
		$helper->SMessage(5,$_SERVER['HTTP_REFERER'],"no","您的链接有误,请务修改链接.");
		exit();
	}
	if(md5($user['password'].$user['key'].$user['visits'])!=$code){
		$helper->SMessage(5,domain,"no","这个链接已过期,5秒前往首页.");
		exit();
	}
	$member->UMember(array('set'=>array('password ='=>$password),'where'=>array('id ='=>$user['id'])));
	$helper->SMessage(5,domain.'view/Login.php',"yes","密码重置成功,5秒前往登录页.");
}else{
	$captcha=$base->library('captcha');
	$mail=$base->post('mail',true);
	if(!$captcha->check($base->post('captcha',true))){
		$helper->SMessage(5,$_SERVER['HTTP_REFERER'],"no","验证码错误,5秒后返回上一页",true);
		exit();
	}
	if(!$helper->CMail($mail)){
		$helper->SMessage(5,$_SERVER['HTTP_REFERER'],"no","您输入的参数有误,5秒后返回上一页",true);
		exit();
	}
	$member=$base->model('member');
	$user=reset($member->GMember(array('select'=>array('id','password','key','visits'),'where'=>array('mail ='=>$mail))));
	if(empty($user)){
		$helper->SMessage(5,$_SERVER['HTTP_REFERER'],"no","您输入的帐户不存在,请确认.",true);
		exit();
	}
	$mation=$base->model('information');
	$info=reset($mation->GMation(array('select'=>array('name','IDcard'),'where'=>array('member ='=>$user['id']))));
	$config=$base->model('config');
	$tpl=reset($config->GConfig(array('where'=>array('label='=>'mail','name='=>'forget'))));
	$tpl['value']=preg_replace('!s:(\d+):"(.*?)";!se', "'s:'.strlen('$2').':\"$2\";'",$tpl['value']);
	$tpl=unserialize($tpl['value']);
	$email=$base->library('email');
	$email->IsSMTP();
	$email->AddAddress($mail,$info['name']);
	$email->Subject =$tpl['subject'];
	$email->MsgHTML($config->parse($tpl['content'],array('member'=>$user['id'],'name'=>$info['name'],'code'=>md5($user['password'].$user['key'].$user['visits']))));
	$email->Send();
	$helper->SMessage(5,domain,"yes","已发送邮件到您的邮箱,请前往您的邮箱查看.");
}
?>